package com.meichis.yndms.ui.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.meichis.mcsappframework.utils.Tools;
import com.meichis.mcsappframework.widget.BadgeView;
import com.meichis.yndms.MCApplication;
import com.meichis.yndms.R;
import com.meichis.yndms.databinding.FragmentDataCentreBinding;
import com.meichis.yndms.dialog.AppUpdateDialog;
import com.meichis.yndms.dialog.SyncBaseDataDialog;
import com.meichis.yndms.model.YIQIYing;
import com.meichis.yndms.ui.activity.YiQiYingActivity;
import com.meichis.yndms.ui.common.BaseFragment;
import com.meichis.yndms.vm.AppUpdateVM;
import com.meichis.yndms.vm.MainVM;
import com.meichis.yndms.vm.SyncBaseDataVM;


/**
 * A simple {@link Fragment} subclass.
 */
public class DataCentreFragment extends BaseFragment {

    private FragmentDataCentreBinding binding;
    private AppUpdateVM vm;
    private SyncBaseDataVM syncVM;
    private MainVM mainVM;
    private BadgeView badgeView;
    private String YiQiYingUrl;

    @Override
    protected View onCreateDataBindingView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_data_centre, container, false);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(AppUpdateVM.class);
        syncVM = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(SyncBaseDataVM.class);
        mainVM = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getActivity().getApplication())).get(MainVM.class);

        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
        return binding.getRoot();
    }

    @Override
    protected void init() {
        badgeView = BadgeView.getBadgeView(getContext(), binding.tvFeedBack);
        badgeView.setGravityOffset(10, -4, true);
        badgeView.setBadgeTextSize(8, true);
        badgeView.setBadgePadding(8, true);
        badgeView.setBadgeGravity(Gravity.END | Gravity.TOP);

        addObserve();
    }

    @Override
    public void onResume() {
        super.onResume();
        mainVM.GetFeedbackPopCount();
        mainVM.GetYQYLinkUrl();
    }

    private void addObserve() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));
        syncVM.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));
        vm.getAppVersion().observe(this, appVersion -> {
            if (appVersion == null) {
                vm.GetLastVersionInfo();
                return;
            }
            /*获取系统版本号*/
            util.setObjectValue(SharePreferenceUtil.PREFERENCES_SERVERVERISON, appVersion);
            if (appVersion.getCurrentVersion() > Tools.getPackageInfo(MCApplication.getContext()).getLongVersionCode()) {
                new AppUpdateDialog(getContext(), appVersion, aVoid -> null).show();
                return;
            }
            //当为最新版本时，提示
            showMsg("当前已是最新版本！");
        });

        mainVM.getFeedBackCount().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                badgeView.setBadgeNumber(integer);
            }
        });

        mainVM.getYiQiYing().observe(this, new Observer<YIQIYing>() {
            @Override
            public void onChanged(YIQIYing yiqiYing) {
                YiQiYingUrl = yiqiYing.getUrl();
                if (yiqiYing.isShow()) {
                    binding.tvYiqiying.setVisibility(View.VISIBLE);
                } else {
                    binding.tvYiqiying.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    public class ClickEvent {

        public void toActivity(String className) {
            Intent intent = getActivity().getIntent();
            intent.setClassName(getActivity(), "com.meichis.yndms.ui.activity." + className);
            startActivity(intent);
        }

        public void gotoYiQiYing() {
            Intent intent = new Intent();
            intent.setClass(getContext(), YiQiYingActivity.class);
            intent.putExtra("url", YiQiYingUrl);
            startActivity(intent);
        }

        public void onAppUpdata() {
            vm.GetLastVersionInfo();
        }

        public void onSyncBaseData() {
            new SyncBaseDataDialog(getContext()).show();
            //  syncVM.GetVisitTemplateList();
        }

        public void onOutApp() {
            MessageTools.ShowFinishMessage(getActivity(), "请确认是否退出应用？");
        }

    }
}
